import Vue from 'vue'
import {getSourceState} from "../list/list_funs"

// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 60,
  currentPage: 1
};

let tableColumn1 = function (self) {
  return {
    title: '综合操作',
    key: 'action',
    fixed: self.$yMobile()?'none':'right',
    width: 200,

    render: (h, params) => {
      return h('div', [
       self.$authority('PRODUCT-1-210')? h('Button', {
        props: {type: 'primary', size: 'small', icon: "md-create"},
        style: {marginRight: '10px',marginTop:'6px',marginBottom:'6px'},
        on: {
          click: (e) => {
            self.editItem(params.index);
          }
        }
      }, '编辑') : h('div',''),
        self.$authority('PRODUCT-1-211')? h('Button', {
          props: {type: 'error', size: 'small', icon: "md-trash"},
          style: {marginRight: '10px',marginTop:'6px',marginBottom:'6px'},
          on: {
            click: () => {
              self.deleteItem(params.index)
            }
          }
        }, '删除') : h('div',''),
        self.$authority('PRODUCT-1-212')? h('Button', {
          props: {type: 'info', size: 'small', icon: "md-flash"},
          style: {marginRight: '10px',marginTop:'6px',marginBottom:'6px'},
          on: {
            click: () => {
              self.upOrDownItem(params.index)
            }
          }
        }, params.row.state === 0 ? '下架' : "上架"): h('div',''),
        self.$authority('PRODUCT-1-213')?h('Button', {
          props: {type: 'success', size: 'small'},
          style: {marginRight: '10px',marginTop:'6px',marginBottom:'6px'},
          on: {
            click: () => {
              self.topOrBottomItem(params.index)
            }
          }
        }, params.row.sequence === 100 ? '取消置顶' : "一键置顶"): h('div','')
      ]);
    },
    align: 'center'
  }
};

const validateProductCode = (rule, value, callback) => { //产品编码校验
  if(value==null || value=="") {
    callback(new Error('产品编号不能为空！'));
  }else{
    let codeReg = /^[A-Za-z0-9\-]+$/;
    if(!codeReg.test(value)){
      callback(new Error('产品编号只能包含“英文字母、数字和-”！'));
    } else {
      callback();
    }
  }
};

let status = {
  topTitle: '',//批量置顶的title
  topHidden: '',//批量置顶的开关
  saleTitle: '',//上下架的title
  //查看图片
  modalImage: {
    isShow: false,
    url: ''
  },
  //各种操作时的 modal
  modalAction: {
    isShow: false,
    isLoading: false,
    title: '',
    content: '',
    type: 0,//0:删除 1:上下架 2:置顶 3:批量删除  4:批量上下架  5:批量置顶  6:批量添加标签 7:上下架色卡 8:删除色卡
    product: {
      id: '',
      code: '',
      sequence:0
    },
    isTop:true
  },
  modalModify: {
    isShow: false,
    isLoading: false,
    isShowCategory: false,
    isEditDetail: true,
  },
  modalSequence: {
    isShow: false,
    isError:false,
    errorMsg:""
  },
  modalOrder: {
    isShow: false,
  },
  modalAdd: {
    isShow: false,
    width: 700,
    isClassificationTreeModelShowing: false,
    isShowAllCategories: false,
    validateRules: {
      productCode: [
        {validator: validateProductCode, trigger: 'blur'},
        {validator: validateProductCode, trigger: 'change'},
      ],
      classificationIds: [
        {
          validator:(rule, value, callback)=>{
            if(value.length<=0){
              callback(new Error('请选择产品分类！'));
            }else{
              callback();
            }
          }, trigger: 'change'},
      ],
    }
  },
  modalExportERP: {
    isShow: false,
  },
  modalStockEmpty: {
    isShow: false,
  },
};

// 页面的数据源
let data = {
  productList: [],
  //编辑详情的色卡
  allTabs: [],
  allSysSize: [],
  allSysSizeMap:{},
  classificationTree:[],
  classificationList:[],
  categoryList:[],
  orderList:[],
  classificationTreeShow: [],
  sourceStates: [
    {value: 1, label: "正常"},
    {value: 2, label: "部分下架"},
    {value: 3, label: "全部已下架"},
  ]
};

  let tableTitles  = {
    columnSelection: {
      type: 'selection',
      width: 60,
      align: 'center',
      fixed: 'left'
    },
    index :{
      type: 'index',
      title: '#',
      width: 70,
      key: 'index',
      fixed: 'left',
      align: 'center',
    },
    columnCode: {
      title: 'SKU',
      key: 'code',
      width: 200,
      align: 'center',
      render: (h, params) => {
        let productUrl = Vue.prototype.$tiyProductUrl(params.row.code);
        return h('div',[h('a',{
          attrs:{href:productUrl,target:'_blank'}
        },params.row.code)]);
      }
    },
    columnImg:function (self) {
      return{
        title: '产品图片',
        key: 'listImgUrl',
        width: 130,
        render: (h, params) => {
          let imageUrl = params.row.productImg;
          let bigImg = params.row.productImg;
          if(params.row.productShowImg!=null&&params.row.productShowImg.zipImgs!=null){
            let zipImg = params.row.productShowImg.zipImgs;
            zipImg.forEach(function (item){
              if(item.size=="200w"){
                imageUrl=self.yunPath+item.imagePath;
              }
            })
          }
          return !imageUrl ? '' : h('img', {
            attrs: {src: imageUrl, height: 60},
            on: {
              click: () => {
                status.modalImage.url = bigImg;
                status.modalImage.isShow = true;
              }
            }
          })
        },
        align: 'center'
      }
    },
    columnPrice: {
      title: '价格',
      key: 'prices',
      width: 150,
      align: 'center',
      render: (h, params) => {
        let price = "";

        if(!params.row.minPrice){ //正式环境中只要配置了价格就一定有minPrice
          return h('div', price);
        }

        let priceArray = [];
        priceArray.push("$" + params.row.minPrice);
        if(params.row.currentPrice != params.row.minPrice){ //不存在有minPrice没有currentPrice的情况
          priceArray.push("$" + params.row.currentPrice);
        }

        price = priceArray.join(" - ");
        return h('div', price);
      }
    },
    columnMinorder: {
      title: '起订量',
      key: 'minOrder',
      width: 80,
      align: 'center',

    },
    columnOrder:function (self){
      return{
      title: '订单数',
      key: 'orderCount',
      width: 100,
      align: 'center',
      render: (h, params) => {
        return h('div',[h('a',{
          on: {
            click: () => {
              self.showOrderList(params.index)
            }
          }
        },params.row.orderCount)]);
      }
      }
    },
    columnSequence: {
      title: '排序',
      key: 'sequence',
      width: 100,
      align: 'center'
    },
    columnCreateTime: {
      title: '创建时间',
      key: 'createtime',
      sortable: 'custom',
      width: 150,
      render: (h, params) => {
        return h('div',Vue.prototype.$tiyBJTime(params.row.createtime));
      },
      align: 'center'
    },
    columnUpTime: {
      title: "上/下架时间",
      key: 'lastmodify',
      sortable: 'custom',
      width: 150,
      render: (h, params) => {
        return h('div',Vue.prototype.$tiyBJTime(params.row.lastmodify));
      },
      align: 'center'
    },
    sourceState: {
      title: "货源状态",
      key: "sourceState",
      width: 120,
      render: (h, params) => {
        let state = params.row.sourceState;
        return h("div", getSourceState(state));
      },
      align: "center"
,    },
    columnEdit1: tableColumn1,
  } ;

let tableOrderTitles = function(self){
  return[
  {
    type: 'index',
    title: '#',
    width: 80,
    align: 'center'
  },
  {
    title: '订单号',
    key: "serialNumber",
    align: 'center',
    width: 200,
    render: (h, params) => {
      return  h('div', [h('a', {
          on: {
            click: () => {
              self.showOrderDetail(params.row.id);
            }
          }
        }, params.row.serialNumber)]);
    }
  },
  {
    title: '国家',
    key: 'countryName',
    align: 'center',
    width: 100
  },
  {
    title: '订单项数量',
    key: 'itemCount',
    align: 'center',
    width: 120
  },
  {
    title: '金额($)',
    key: 'actualPayment',
    align: 'center',
    width: 160
  },
  {
    title: '下单时间',
    align: 'center',
    width: 200,
    render: (h, params) => {
      return h('div', self.$tiyBJTime(params.row.createtime));
    }
  },
  {
    title: '订单状态',
    align: 'center',
    render: (h, params) => {
      let state = '';
      self.$store.getters.dictionaries.orderStateDict.forEach(function (item) {
        if (params.row.state === item.value) {
          state = item.name;
        }
      });
      return h('div', state);
    }
  },
 ]
} ;



// 和网络交互相关的参数
let params = {
  //查询相关
  productCode: "",
  collectionProductCode: "",
  gender: "",
  choosedCategory: "",
  classifyLevel: "",
  orderBy: "",
  is_prefer: "0",//上下架
  isTop: true,//根据选择的产品得出 现在置顶的操作
  sourceState: null,
  //选拔的产品
  products: [],
  //上传图片的folder
  uploadFolder: {},
  //当前选中的标签
  productTabs: 'product',
  squence: {
    squenceNumber: 9999,
    choosedCategory: "",
    choosedLevel: 1
  },
  paramProduct: {
    productCode: "",
    classificationIds: [],
  },
  exportERP: {
    startDate: null,
    endDate: null,
    categoryId: '',
    categoryLevel: 1
  },
};



let prepareTable = function(self){

  let titles  = [
    tableTitles.columnSelection,
    tableTitles.index,
    tableTitles.columnCode,
    tableTitles.columnImg(self),
    tableTitles.columnPrice,
    tableTitles.columnMinorder,
    self.$authority('PRODUCT-1-205')?tableTitles.columnOrder(self):null,
    tableTitles.columnSequence,
    tableTitles.columnCreateTime,
    tableTitles.columnUpTime,
    tableTitles.sourceState,
    tableTitles.columnEdit1(self)
  ];

  return self.$authTableTitles(titles);

};



export  default  {
  tPage: page,
  tStatus: status,
  tParam: params,
  tData: data,
  tTable: prepareTable,
  tRole: {},
  tableOrderTitles:tableOrderTitles
};
